#!/usr/bin/perl -w
#
# Copyright 2004 Hirotaka Ogawa. This code cannot be rediributed without
# permission.
#
# mt-resave-entries.cgi: resave all entries without basenames.

use strict;
local $|=1;

my($MT_DIR);
BEGIN {
    if ($0 =~ m!(.*[/\\])!) {
        $MT_DIR = $1;
    } else {
        $MT_DIR = './';
    }
    unshift @INC, $MT_DIR . 'lib';
    unshift @INC, $MT_DIR . 'extlib';
}

use MT;
use MT::Entry;
use MT::ConfigMgr;

print "Content-Type: text/html\n\n";
print <<HTML;
<html>
<head><title>Movable Type Re-save Tool</title></head>
<body>
<h1>Movable Type Re-save Tool</h1>

<pre>
HTML

my $mt = MT->new;
my $iter = MT::Entry->load_iter(undef, {sort=>'id',direction=>'ascend'});
while (my $o = $iter->()) {
    if (!$o->basename) {
	$o->save
	  or printf ("[ERR] %s at MT::Entry->id: [%s]\n", $o->errstr, $o->id);
	printf("Entry [%s] title=%s basename=%s\n", $o->id, $o->title, $o->basename);
    }
}

print <<HTML;
</pre>
<p><strong>Resaved successfully.</strong></p>
</body>
</html>
HTML
